package com.fish.yssb.ui.page.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.db.bean.BaseInfoTable;

import java.text.DecimalFormat;
import java.util.ArrayList;


/**
 * Created by WangChang on 2016/4/1.
 */
public class realtimeYMListAdapter extends RecyclerView.Adapter<realtimeYMListAdapter.BaseViewHolder> {
    private ArrayList<BaseInfoTable> dataList = new ArrayList<BaseInfoTable>();
    private Resources res;
    private Context m_context;
    public void replaceAll(ArrayList<BaseInfoTable> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        m_context =parent.getContext();
        return new realtimeYCViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rt_ym_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

        holder.setData(dataList.get(position),position);
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(BaseInfoTable data,int pos) {
        }
    }



    private class realtimeYCViewHolder extends realtimeYMListAdapter.BaseViewHolder {


        private TextView tv_name;
        private TextView tv_addr;
        private TextView tv_val;
        private TextView tv_unit;
        private LinearLayout ll_bj;
        public realtimeYCViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_addr = view.findViewById(R.id.tv_addr);
            tv_val = view.findViewById(R.id.tv_val);
            tv_unit = view.findViewById(R.id.tv_unit);
            ll_bj = view.findViewById(R.id.ll_bj);
        }

        @Override
        void setData(BaseInfoTable data,int pos) {
            if (data != null) {
                if(pos%2==1)
                {
                    ll_bj.setBackgroundColor(Color.parseColor("#eeeeee"));
                }else{
                    ll_bj.setBackgroundColor(Color.parseColor("#ffffff"));
                }
                tv_name.setText(data.getName());
                tv_addr.setText(data.getConnAdr());
//                float a=(float) (Math.random()*1000);
                tv_unit.setText(data.getUnit());
                if(data.getDefualtVal()==null)
                {
                    tv_val.setText("");
                    return;
                }

                String numb= data.getDefualtVal();

                tv_val.setText(numb);

            }


        }
    }

}
